package com.bjtu.douyin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjtu.common.pojo.Page;
import com.bjtu.common.pojo.Video;
import com.bjtu.common.pojo.VideoLike;
import com.bjtu.common.util.Result;
import com.bjtu.douyin.mapper.VideoLikeMapper;
import com.bjtu.douyin.mapper.VideoMapper;
import com.bjtu.douyin.service.VideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class VideoServiceImpl implements VideoService {
    private final VideoMapper videoMapper;

    private final VideoLikeMapper videoLikeMapper;

    @Override
    public Result<?> addVideo(Video video) {
        videoMapper.insert(video);
        return Result.success();
    }

    @Override
    public Result<?> deleteVideo(Integer id) {
        // 逻辑删除
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Video video = new Video();
        video.setStatus(2);
        videoMapper.update(video, queryWrapper);
        return Result.success();
    }

    @Override
    public Result<?> getVideo(Integer id, Integer userId) {
        Video video = videoMapper.selectById(id);

        if (video != null) {
            QueryWrapper<VideoLike> likeWrapper = new QueryWrapper<>();
            likeWrapper.eq("video_id", id).eq("user_id", userId);
            int likeCount = videoLikeMapper.selectCount(likeWrapper).intValue(); // 转换为int类型
            video.setIsLiked(likeCount);
        }

        return Result.success(video);
    }

    @Override
    public Result<?> likeVideo(Integer id, Integer userId) {
        Video video = videoMapper.selectById(id);
        Integer number = video.getLikeNumber();
        number++;
        video.setLikeNumber(number);
        videoMapper.updateById(video);

        VideoLike videoLike = new VideoLike();
        videoLike.setVideoId(id);
        videoLike.setUserId(userId);
        videoLikeMapper.insert(videoLike);

        return Result.success();
    }

    @Override
    public Result<?> dislikeVideo(Integer id, Integer userId) {
        Video video = videoMapper.selectById(id);

        QueryWrapper<VideoLike> likeWrapper = new QueryWrapper<>();
        likeWrapper.eq("video_id", id).eq("user_id", userId);
        videoLikeMapper.delete(likeWrapper);

        Integer number = video.getLikeNumber();
        number--;
        video.setLikeNumber(number);
        videoMapper.updateById(video);
        return Result.success();
    }

    @Override
    public Result<?> getVideoList(Integer userId, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("status", 1);
        List<Video> videoList = videoMapper.selectList(queryWrapper);

        // 遍历videoList，设置每一个Video对象的isLike属性
        for (Video video : videoList) {
            QueryWrapper<VideoLike> likeWrapper = new QueryWrapper<>();
            likeWrapper.eq("video_id", video.getId()).eq("user_id", userId);
            int likeCount = videoLikeMapper.selectCount(likeWrapper).intValue(); // 转换为int类型
            video.setIsLiked(likeCount);
        }

        PageInfo<Video> pageInfo = new PageInfo<>(videoList);
        Page resultPage = new Page(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
        return Result.success(resultPage);
    }
}
